package com.example.test_1_21.mqserver.datacenter;

import com.example.test_1_21.TestApplication;
import com.example.test_1_21.mqserver.core.Binding;
import com.example.test_1_21.mqserver.core.Exchange;
import com.example.test_1_21.mqserver.core.ExchangeType;
import com.example.test_1_21.mqserver.core.MSGQueue;
import com.example.test_1_21.mqserver.mapper.MetaMapper;

import java.io.File;
import java.util.List;

//通过这个类来整合上述的数据库操作
public class DataBaseManager {
    //从spring中拿到现成的对象
    private MetaMapper metaMapper;

    //针对数据库进行初始化
    public void init(){
        //手动的获取到MetaMapper
        metaMapper= TestApplication.context.getBean(MetaMapper.class);
        if(!checkDBExists()){
            //数据库不存在，进行建库建表操作
            //创建数据库
            File dataDir=new File("./data");
            dataDir.mkdirs();
            //创建数据表
            createTable();
            //插入默认数据
            createDefault();
            System.out.println("[DataBaseManager] 数据库初始化完成");
        }else{
            //数据库已经存在
            System.out.println("[DataBaseManager] 数据库已存在");
        }
    }

    public void deleteDB(){
        File file=new File("./data/meta.db");
        boolean ret=file.delete();
        if (ret){
            System.out.println("[DataBaseManager] 删除数据库文件成功！");
        }else{
            System.out.println("[DataBaseManager] 删除数据库文件失败！");
        }

        File dataDir=new File("./data");
        ret=dataDir.delete();
        if (ret){
            System.out.println("[DataBaseManager] 删除数据库目录成功！");
        }else{
            System.out.println("[DataBaseManager] 删除数据库目录失败！");
        }
    }

    private boolean checkDBExists(){
        File file=new File("./data/meta.db");
        if(file.exists()){
            return true;
        }
        return false;
    }

    //用来建表。建库操作不需要手动执行，会自动创建meta.db文件
    private void createTable(){
        metaMapper.createExchangeTable();
        metaMapper.createQueueTable();
        metaMapper.createBindingTable();
        System.out.println("[DataBaseManager] 创建表完成");
    }

    //给数据库表中，添加默认的数据库
    //此处主要是添加一个默认的交换机
    //RabbitMQ里有一个这样的匿名的交换机，类型是DIRECT
    private void createDefault(){
        //构造一个默认的交换机
        Exchange exchange=new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);
        metaMapper.insertExchange(exchange);
        System.out.println("[DataBaseManager] 创建初始数据完成");
    }

    //封装数据库的其他操作
    public void insertExchange(Exchange exchange){
        metaMapper.insertExchange(exchange);
    }
    public List<Exchange> selectAllExchanges(){
        return metaMapper.selectAllExchanges();
    }
    public void deleteExchange(String exchangeName){
        metaMapper.deleteExchange(exchangeName);
    }

    public void insertQueue(MSGQueue queue){
        metaMapper.insertQueue(queue);
    }
    public List<MSGQueue> selectAllQueues(){
        return metaMapper.selectAllQueues();
    }
    public void deleteQueue(String queueName){
        metaMapper.deleteQueue(queueName);
    }

    public void insertBinding(Binding binding){
        metaMapper.insertBinding(binding);
    }
    public List<Binding> selectAllBindings(){
        return metaMapper.selectAllBindings();
    }
    public void deleteBinding(Binding binding){
        metaMapper.deleteBinding(binding);
    }
}
